package integration

import nancal.mp.db.mongo.mor
import nancal.mp.service.git.impl.GitEaService
import nbcp.base.annotation.log.MyLogLevel
import nbcp.base.enums.LogLevelScopeEnum
import nbcp.myoql.db.mongo.query
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired


//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class GitTest : TestBase() {

    @Autowired
    lateinit var gitEaService: GitEaService

    @Test
    @MyLogLevel(LogLevelScopeEnum.WARN)
    fun scan_code_line() {
        var gitServer = mor.dev.gitServer.query().toEntity();
        var list1 = mutableListOf<String>()
        var list2 = mutableListOf<String>()
        gitEaService.getOrgs(gitServer!!)
            .data.forEach { orgData ->
                var org = orgData.name;

                list1.add("")
                list1.add("# Org ${org}:")

                list1.add("mkdir -p ${org}; cd ${org}")
                gitEaService.getRepos(gitServer, org)
                    .data
                    .forEach {
                        if (it.archived) {
                            println("# ${it.cloneUrl}, 仓库已归档, 跳过!");
                            return@forEach
                        }

                        if (it.size > 1024 * 1024) {
                            println("# ${it.cloneUrl}, 仓库大于1G, 跳过!");
                            return@forEach
                        }

                        list1.add("\t git clone ${it.cloneUrl}")

                        var f = it.cloneUrl.split("/").last().split(".").first();
//                        list2.add("\t tar -czvf tar/${org}/${f}.tar.gz  ${org}/${f}")

                        list1.add("\t mkdir -p ../../tar/${org}")
                        list1.add("\t tar -czvf ../../tar/${org}/${f}.tar.gz  ${f}")
                        list1.add("\t rm -rf ${f}")
                    }
                list1.add("cd ..")
            }


        println(list1.joinToString("\n"))
        println(list2.joinToString("\n"))
    }

}